package com.service.bracelet.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.bracelet.common.CommonResult;
import com.service.bracelet.common.CommonResultPage;
import com.service.bracelet.dao.UserMapper;
import com.service.bracelet.entity.User;
import com.service.bracelet.service.IUserService;
import com.service.bracelet.util.SecurityUtil;
import com.service.bracelet.util.UtilConsts;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public CommonResult<List<User>> queryList(Map<String, Object> params) {
        CommonResultPage.setPageParams(params);
        CommonResultPage<List<User>> page = new CommonResultPage(params);
        if (!page.existTotal()) {
            page.setTotal(userMapper.queryListTotal(params));
        }
        return page.setData(userMapper.queryList(params));
    }

    public User getUserByAccount(Map<String, Object> params) {
        return userMapper.getUserByAccount(params);
    }

    public User getUserById(Map<String, Object> params) {
        return userMapper.getUserById(params);
    }

    @Override
    public void updatePassword(Map<String, Object> params) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwd = MapUtils.getString(params, UtilConsts.PARAMS_PASSWORD);
        params.put(UtilConsts.PARAMS_PASSWORD, SecurityUtil.encryption(pwd));
        userMapper.updatePassword(params);
    }

    @Override
    public void resetPassword(Map<String, Object> params) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        params.put(UtilConsts.PARAMS_PASSWORD, SecurityUtil.encryption(UtilConsts.SECURITY_DEFAULT_PASSWORD));
        userMapper.updatePassword(params);
    }

    @Override
    public void updateInfo(Map<String, Object> params) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        params.put(UtilConsts.PARAMS_UPDATETIME, new Date());
        String answer = MapUtils.getString(params, UtilConsts.PARAMS_ANSWER);
        if (StringUtils.isNotBlank(answer)) {
            params.put(UtilConsts.PARAMS_ANSWER, SecurityUtil.encryption(answer));
        }
        userMapper.updateInfo(params);
    }

    @Override
    public void register(Map<String, Object> params) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwd = MapUtils.getString(params, UtilConsts.PARAMS_PASSWORD);
        params.put(UtilConsts.PARAMS_PASSWORD, SecurityUtil.encryption(pwd));
        String answer = MapUtils.getString(params, UtilConsts.PARAMS_ANSWER);
        if (StringUtils.isNotBlank(answer)) {
            params.put(UtilConsts.PARAMS_ANSWER, SecurityUtil.encryption(answer));
        }
        userMapper.register(params);
    }

    @Override
    public boolean answerQuestion(Map<String, Object> params) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        User user = userMapper.getUserForVerify(params);
        if (user == null) {
            return false;
        }
        int questionId = MapUtils.getInteger(params, UtilConsts.PARAMS_QUESTIONID);
        String answer = MapUtils.getString(params, UtilConsts.PARAMS_ANSWER);
        return questionId == user.getQuestionId() && SecurityUtil.validate(answer, user.getAnwser());
    }
}
